#include <QtGui>
#include "Delegate.h"
#include <QStandardItemModel>
#include "../Settings/GameSettings.h"

SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    : QItemDelegate(parent)
{
}

QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &opt/* option */,
    const QModelIndex &mi/* index */) const
{
    QSpinBox *editor = new QSpinBox(parent);
    QPoint range = GameSettings::me()->integerSettingRange(Game::Setting(mi.row()));
    editor->setMinimum(range.x());
    editor->setMaximum(range.y());

    return editor;
}

void SpinBoxDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    //int value = index.model()->data(index, Qt::EditRole).toInt();

    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(GameSettings::me()->setting(Game::Setting(index.row())).value<uint>());
}

void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->interpretText();
    int value = spinBox->value();

    model->setData(index, value, Qt::EditRole);
}

void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}

